/**
 * Created by rong on 16/9/6.
 */
require.config(require_config);
define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var goods_id = getUrlPath(3);
    var site_id = getUrlPath(4);
    var id = getUrlPath(5);
    var card;
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            this.fetch();
            this.event();
        },
        fetch: function () {
            $.ajax({
                url: _global.url.api + 'sign_up_info',
                data: {goods_id: goods_id, site_id: site_id,id:id},
                type: 'get',
                async: true,
                success: function (res) {
                    console.log("res ,", res);
                    if (res.code == 0) {
                        var html = template('data-item', res.data);
                        $("#data-list").html(html);
                        card = res.data;
                    }
                }
            });

        },
        event: function () {
            this.data = {};
            var _this = this;

            $('input,select,textarea').on('change', function (e) {
                _this.change(e);
            });
            //上传图片
            $('body').on('click', '.btn-upload', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        img.item = item;
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
            $('body').on('click', '#refused', function () {
                var status = $("#refused").val();
                if (status == 2) {
                    $('#item-check').html(template('check-tpl', {}));
                    $('#item-footer').html(template('refused-tpl', {}));
                }
            });
            $('body').on('click', '#passed', function () {
                var status = $("#passed").val();
                if (status == 1) {
                    $('#item-check').html('');
                    $('#item-footer').html(template('passed-tpl', {}));
                }
            });
            $('body').on('click', '.btn-save1', function () {
                _this.data.status = $("input[name='check']:checked").val();
                _this.data.intro = $("input[name='intro']").val();
                _this.data.goods_id = goods_id;
                _this.data.id = id;
                _this.data.site_id = site_id;
                if (new Model({
                        url: _global.url.api + 'check_sign_up',
                        data: _this.data
                    }).put()) {
                    dialog({
                        'title': '制作卡片',
                        'content': template('add-tpl-1', card),
                        'width': 700,
                        ok: function () {
                            var data = _this.data;
                            data.title = $('[name=title]').val();
                            if (data.title == '') {
                                Alert({msg: '请输入标题', type: 'error'});
                                return false;
                            }
                            data.intro = $('[name=intro]').val();
                            if (data.intro == '') {
                                Alert({msg: '请输入备注', type: 'error'});
                                return false;
                            }
                            data.corner_ico = $.trim($('[name="corner_ico"]:checked').val());
                            data.tag_limit = $('[name="tag_limit"]:checked').val();
                            var user_tag_id_arr = new Array();
                            $('[name="user_tag_id"]:checked').each(function (i) {
                                var user_tag_id = $.trim($(this).val());
                                user_tag_id_arr.push(user_tag_id);
                            });
                            var card_cate = new Array();
                            $('[name="card_cate"]:checked').each(function (i) {
                                var card_cate_id = $.trim($(this).val());
                                card_cate.push(card_cate_id);
                            });
                            if (data.tag_limit == 1) {
                                if (user_tag_id_arr.length < 1) {
                                    Alert({msg: '请选择用户标签', type: 'error'});
                                    return false;
                                }
                            }
                            data.user_tag_id = user_tag_id_arr;
                            data.card_cate = card_cate;

                            data.url = $('[name=url]').val();
                            data.content = $.trim($('[name=content]').val());
                            data.image = $('[name=image]').val();
                            data.width = $.trim($('[name=width]').val());
                            data.height = $.trim($('[name=height]').val());
                            if (data.width == '') {
                                Alert({msg: '请输入宽度', type: 'error'});
                                return false;
                            }
                            if (data.height == '') {
                                Alert({msg: '请输入高度', type: 'error'});
                                return false;
                            }
                            if (data.image == '') {
                                Alert({msg: '请上传图片', type: 'error'});
                                return false;
                            }
                            console.log(data);
                            if (new Model({
                                    url: _global.url.api + 'promotion_card',
                                    data: data
                                }).post()) {
                                return true;
                            }
                        }, cancel: function () {
                            location.href = '/admin/promotion_sign_up';
                        }
                    }).show();
                }
            });
            //$(".btn-save2").on("click", function () {
            $('body').on('click', '.btn-save2', function () {
                _this.data.status = $("input[name='check']:checked").val();
                _this.data.intro = $("input[name='intro']").val();
                _this.data.goods_id = goods_id;
                _this.data.id = id;
                _this.data.site_id = site_id;
                if (_this.data.intro == '' || _this.data.intro == 'undefined') {
                    Alert({msg: '请备注信息', type: 'error'});
                    return false;
                }

                if (new Model({
                        url: _global.url.api + 'check_sign_up',
                        data: _this.data
                    }).put()) {
                    //location.href = '/site/groupbuy';
                }
            });
            $('body').on('click', '[name="tag_limit"]', function (e) {
                var tag_limit = $('[name="tag_limit"]:checked').val();
                if (tag_limit == 1) {
                    $('.card_user_tag').show();
                } else {
                    $('.card_user_tag').hide();
                }
            });

            //保存
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });

        },

        imgPreview: function (img) {
            if (typeof img.id !== 'undefined') {
                $('#img-view').html(template('img-tpl', img));
            }
        },
    };

    new index();
});